package com.devilvan.customSort;


import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class CustomSortMapper extends Mapper<LongWritable, Text, FlowSortBean,NullWritable> {
    private FlowSortBean flowSortBean;
    private Text text;

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        flowSortBean = new FlowSortBean();
        text = new Text();
    }

    public enum MapperEnum {
        CUSTOM_COUNTER_MAPPER("COUNTER_MAPPER_GROUP","COUNTER_MAPPER_NAME");

        private final String group;
        private final String name;

        MapperEnum(String group, String name) {
            this.group = group;
            this.name = name;
        }

        public String getGroup() {
            return group;
        }

        public String getName() {
            return name;
        }
    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 通过context获取计数器对象，统计调用Mapper的次数
        Counter counter = context.getCounter(MapperEnum.CUSTOM_COUNTER_MAPPER.getGroup(),
                MapperEnum.CUSTOM_COUNTER_MAPPER.getName());
        counter.increment(1L);

        String[] field = value.toString().split("\t");
        flowSortBean.setMsiSdn(field[0]);
        flowSortBean.setUpPackNum(Long.valueOf(field[1]));
        flowSortBean.setDownPackNum(Long.valueOf(field[2]));
        flowSortBean.setUpPayLoad(Long.valueOf(field[3]));
        flowSortBean.setDownPayLoad(Long.valueOf(field[4]));
        context.write(flowSortBean,NullWritable.get());
    }
}
